package com.jzh.cmhSystem.controller;

import com.jzh.cmhSystem.biz.EnterpriseService;
import com.jzh.cmhSystem.biz.SectionService;
import com.jzh.cmhSystem.biz.UserAndDetailService;
import com.jzh.cmhSystem.biz.UserService;
import com.jzh.cmhSystem.entity.*;
import com.jzh.cmhSystem.utils.ThreadLocalUtil;
import io.swagger.annotations.Api;
import io.swagger.v3.oas.annotations.Operation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


//@Tag(name = "验证码相关", description = "这是描述")
@Api(tags = "管理员相关", value = "这是描述")
@RestController
@RequestMapping("/api/admin")
//@Slf4j
public class AdminController {


    @Autowired
    private UserAndDetailService userAndDetailService;

    @Autowired
    private SectionService sectionService;
    @Operation(summary = "普通用户列表查询")
    @GetMapping("/get_user_list_by_name")
    public Result<List<UserAndDetail>> getUserListByName(
//            联系人，电话，租户名称
            @RequestParam(required = false, defaultValue = "") String nickname,
            @RequestParam(required = false, defaultValue = "") String phone,
            @RequestParam(required = false, defaultValue = "") String name
    ) {

        return Result.success(userAndDetailService.getUserListByRoleAndName(nickname, phone, name, null, null));
    }


    @Operation(summary = "企业用户列表查询")
    @GetMapping("/get_en_user_list_by_name")
    public Result<List<UserAndDetail>> getEnUserListByName(
//            联系人，电话，租户名称
            @RequestParam(required = false, defaultValue = "") String nickname,
            @RequestParam(required = false, defaultValue = "") String phone,
            @RequestParam(required = false, defaultValue = "") String name
    ) {
        return Result.success(userAndDetailService.getUserEnListByRoleAndName(nickname, phone, name, "1", null));
    }


    @Operation(summary = "获取管理员用户信息")
    @GetMapping("/get_en_user_info_by_id")
    public Result<List<UserAndDetail>> getEnUserInfoByUserId(@RequestParam(required = false, defaultValue = "") String userId) {
        return Result.success(userAndDetailService.getEnUserByUserId(Integer.parseInt(userId)));
    }

    @Operation(summary = "获取用户信息")
    @GetMapping("/get_user_info_by_id")
    public Result<List<UserAndDetail>> getUserInfoById(@RequestParam String userId) {
        return Result.success(userAndDetailService.getUserDetailByUserId(Integer.parseInt(userId)));
    }

    @Operation(summary = "修改用户信息")
    @PutMapping("/update_info")
    public Result updateInfo(@RequestBody UserAndDetail userAndDetail) {
        User user = new User();
        user.setRole(userAndDetail.getRole());
        user.setId(userAndDetail.getUserId());
        userService.updateById(user);

        Section section= sectionService.getSectionById(userAndDetail.getSectionId());
        userAndDetail.setEnterpriseId(section.getEnterpriseId());
        userAndDetailService.updateInfoById(userAndDetail);
        return Result.success("");
    }

    @Operation(summary = "添加企业管理员")
    @PostMapping("/add_en_user")
    public Result register(
            @RequestBody UserAndDetail userAndDetail
    ) {
        User u = userService.findUserByName(userAndDetail.getUsername());
        if (u == null) {
            //没有占用
            //注册
            int userId = userService.register(userAndDetail.getUsername(), userAndDetail.getPassword(), 1);
            //添加个人信息
            userAndDetail.setUserId(userId);
            userAndDetail.setSectionId(null);
            userAndDetail.setPassword(null);
            userAndDetailService.addDetail(userAndDetail);
            Enterprise enterprise = new Enterprise();
            enterprise.setName(userAndDetail.getEnterpriseName());
            enterprise.setContact(userAndDetail.getContact());
            enterprise.setAdminId(userId);
            enterpriseService.addEnterprise(enterprise);

            return Result.success();
        } else {
            //占用
            return Result.error("用户名已被占用");
        }
    }

    @Operation(summary = "修改管理员信息")
    @PutMapping("/update_en_user_info")
    public Result updateEnUserInfo(@RequestBody UserAndDetail userAndDetail) {
        Enterprise enterprise = new Enterprise();
        enterprise.setAdminId(userAndDetail.getUserId());
        enterprise.setName(userAndDetail.getEnterpriseName());
        enterprise.setContact(userAndDetail.getContact());
        enterpriseService.updataByEnUserId(enterprise);

        userAndDetail.setSectionId(null);
        userAndDetail.setPassword(null);
        userAndDetail.setEnterpriseId(null);
        userAndDetailService.updateInfoById(userAndDetail);
        return Result.success("");
    }


    @Autowired
    private UserService userService;


    @Operation(summary = "添加用户")
    @PostMapping("/addUser")
    public Result addUser(
            @RequestBody UserAndDetail userAndDetail
    ) {
        String username = userAndDetail.getUsername();
        String password = userAndDetail.getPassword().trim();
        if (password.isEmpty()) {
            password = "123456";
        }
        User u = userService.findUserByName(username);
        if (u == null) {
            //没有占用 注册初始化信息
            int userId = userService.register(username, password, userAndDetail.getRole());
            userAndDetail.setUserId(userId);
            Section section= sectionService.getSectionById(userAndDetail.getSectionId());
            userAndDetail.setEnterpriseId(section.getEnterpriseId());

            userAndDetailService.addDetail(userAndDetail);
            return Result.success();
        } else {
            //占用
            return Result.error("用户名已被占用");
        }
    }

    @Operation(summary = "删除用户")
    @DeleteMapping("/del_user_by_id")
    public Result deleteUserById(@RequestBody UserAndDetail userAndDetail) {
        userService.deleteUserById(userAndDetail);
        userAndDetailService.deleteUserDetailById(userAndDetail);
        return Result.success("");
    }

    @Autowired
    private EnterpriseService enterpriseService;

    @Operation(summary = "企业列表查询")
    @GetMapping("/getEnterprise")
    public Result<List<Enterprise>> getEnterprise(@RequestParam(required = false, defaultValue = "") String name) {
        return Result.success(enterpriseService.getEnterprisesWithSectionsBySectionName(name));
    }


    @Operation(summary = "获取用户本人信息")
    @GetMapping("/info")
    public Result<List<UserAndDetail>> getInfo() {
        Map<String, Object> map = ThreadLocalUtil.get();
        int id = (int) map.get("id");
        return Result.success(userAndDetailService.getEnUserByUserId(id));
    }

    @Operation(summary = "修改本人信息")
    @PutMapping("/update_my_info")
    public Result updateMyInfo(@RequestBody UserAndDetail userAndDetail) {
        Map<String, Object> map = ThreadLocalUtil.get();
        int id = (int) map.get("id");
        userAndDetail.setUserId(id);
        userAndDetailService.updateInfo(userAndDetail);
        return Result.success("");
    }


}
